package com.bawei.module_main

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.module_main.adapter.MainViewPageAdapter
import com.bawei.module_main.databinding.ActivityMainBinding
import com.bawei.module_message.ui.MessageFragment
import com.bawei.module_mine.mine.MineFragment
import com.bawei.module_notice.ui.NoticeFragment
import com.bawei.module_park.ui.ParkFragment
import com.bawei.module_service.ui.ServiceFragment
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.HiltAndroidApp


/**
 *@Author：lxy
 *@Description:主页面实现底部导航的切换
 *@Time：2025/6/10
 */
@Route(path = Router.Ui.MainActivity)
@AndroidEntryPoint
class MainActivity : BaseActivity<BaseViewModel, ActivityMainBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_main

    override val mViewModel: BaseViewModel by viewModels()

    private var fragments = mutableListOf<Fragment>(
        ParkFragment(),
        ServiceFragment(),
        MessageFragment(),
        NoticeFragment(),
        MineFragment()
    )
    private lateinit var adapter: MainViewPageAdapter

    override fun initView() {

    }

    override fun initListener() {

    }

    override fun initData() {
        adapter = MainViewPageAdapter(this, fragments)
        mBinding.vp.adapter = adapter
        mBinding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mBinding.bottom.selectedItemId = when (position) {
                    0 -> R.id.parkFragment
                    1 -> R.id.serviceFragment
                    2 -> R.id.messageFragment
                    3 -> R.id.noticeFragment
                    4 -> R.id.mineFragment

                    else -> {
                        R.id.parkFragment
                    }
                }
//                mBinding.bottom.menu.getItem(position).isChecked = true
            }
        })

        mBinding.bottom.setOnItemSelectedListener {
            val position = when (it.itemId) {
                R.id.parkFragment -> 0
                R.id.serviceFragment -> 1
                R.id.messageFragment -> 2
                R.id.noticeFragment -> 3
                R.id.mineFragment -> 4

                else -> {
                    0
                }
            }
            mBinding.vp.setCurrentItem(position, true)
            true
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        // 遍历并移除所有已添加的Fragment
        supportFragmentManager.fragments.forEach { fragment ->
            supportFragmentManager.beginTransaction().remove(fragment).commit()
        }
        fragments.clear()  // 释放所有Fragment引用

    }
}